﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XWord.GUI.Presenters.Fundamental;
using XWord.Fundamental.Domain;
using XWord.GUI.Presenters.Interface;
using Microsoft.Practices.ServiceLocation;
using System.Collections;
using Luna.WPF.ApplicationFramework;

namespace XWord.GUI.Views.Fundamental
{
    /// <summary>
    /// Interaction logic for EtymaCategoryExplorerView.xaml
    /// </summary>
    public partial class EtymaCategoryExplorerView : UserControl
    {
        public EtymaCategoryExplorerView()
        {
            InitializeComponent();
        }

        private void TreeView_Selected(object sender, RoutedEventArgs e)
        {
            var element = e.OriginalSource as TreeViewItem;
            var data = element.DataContext as EtymaLibrary;
            var serviceLocator = ServiceLocator.Current;
            if (data != null && element.ItemsSource == null)
            {
                var wordList = serviceLocator.GetInstance<WordSearchEngine>().FilterWord(data.Name);
                serviceLocator.GetInstance<IEtymaWordListPresenter>().FilertWord = wordList;
                element.ItemsSource = wordList;
                element.IsExpanded = true;
            }
            if (data != null && element.HasItems)
            {
                var presenter = serviceLocator.GetInstance<IEtymaLibraryExplorerPresenter>();
                presenter.SelectedItem = data;
            }
            string wordData = element.DataContext as string;
            if (wordData != null)
            {
                var presenter = serviceLocator.GetInstance<IWordExplorerPresenter>();
                presenter.SelectedItem = wordData;
            }
        }
    }
}